package com.example.designhbwxz.book.controller;

import com.example.designhbwxz.book.adapter.Login3rdTarget;
import com.example.designhbwxz.book.pojo.UserInfo;
import com.example.designhbwxz.book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

/**
 * 适配器模式 - 用户控制层
 * <p/>
 * 测试步骤：将 href 后面的 url 链接复制到浏览器进行访问即可
 * <a href="https://gitee.com/oauth/authorize?client_id=4b0cecba34d0dd94d8d32c76668193c9ea4fea3378946a72a05183418c61fe80&client_secret=90bce274071a26a4752ff67add83fb2c800c0eb6afdea15b205044c4a6e013aa&redirect_uri=http://localhost:8081/user/gitee&response_type=code&state=GITEE">...</a>
 *
 * @author shenzw
 * @date 2023/10/23
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    @Qualifier(value = "userServiceImpl")
    private UserService userServiceImpl;

    @Autowired
    private Login3rdTarget login3rdTarget;

    /**
     * 用户登陆
     *
     * @param account  账号
     * @param password 密码
     * @return String
     */
    @PostMapping("/login")
    public String login(String account, String password) {
        return userServiceImpl.login(account, password);
    }

    /**
     * 注册用户
     *
     * @param userInfo 注册用户信息
     * @return String
     */
    @PostMapping("/register")
    public String register(@RequestBody UserInfo userInfo) {
        return userServiceImpl.register(userInfo);
    }

    /**
     * Gitee 平台回调地址接口，并携带 code 参数和 state 参数
     *
     * @param code  code 码
     * @param state state
     * @return String
     */
    @GetMapping("/gitee")
    public String gitee(String code, String state) {
        return login3rdTarget.loginByGitee(code, state);
    }
}
